package com.dinzeer.lostbladereborn.entity;

import mods.flammpfeil.slashblade.SlashBladeConfig;
import mods.flammpfeil.slashblade.ability.StunManager;
import mods.flammpfeil.slashblade.capability.concentrationrank.ConcentrationRankCapabilityProvider;
import mods.flammpfeil.slashblade.capability.concentrationrank.IConcentrationRank;
import mods.flammpfeil.slashblade.entity.EntityDrive;
import mods.flammpfeil.slashblade.entity.Projectile;
import mods.flammpfeil.slashblade.item.ItemSlashBlade;
import mods.flammpfeil.slashblade.util.AttackManager;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Mth;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.monster.EnderMan;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraftforge.entity.PartEntity;

public class DriveEntityHeal extends EntityDrive {
    public DriveEntityHeal(EntityType<? extends Projectile> entityTypeIn, Level worldIn) {
        super(entityTypeIn, worldIn);
    }

    @Override
    protected void onHitEntity(EntityHitResult entityHitResult) {
        if ( entityHitResult.getEntity() instanceof LivingEntity targetEntity){

        float damageValue = (float)this.getDamage();

        if (this.isOnFire() && !(targetEntity instanceof EnderMan)) {
            targetEntity.setSecondsOnFire(5);
        }

        targetEntity.invulnerableTime = 0;
        Entity var8 = this.getOwner();
        if (var8 instanceof LivingEntity living) {
            damageValue = (float)((double)damageValue * living.getAttributeValue(Attributes.ATTACK_DAMAGE));
            if (living instanceof Player player) {
                IConcentrationRank.ConcentrationRanks rankBonus = player.getCapability(ConcentrationRankCapabilityProvider.RANK_POINT).map((rp) -> rp.getRank(player.getCommandSenderWorld().getGameTime())).orElse(IConcentrationRank.ConcentrationRanks.NONE);
                float rankDamageBonus = (float)rankBonus.level / 2.0F;
                if (IConcentrationRank.ConcentrationRanks.S.level <= rankBonus.level) {
                    int refine = (Integer)player.getMainHandItem().getCapability(ItemSlashBlade.BLADESTATE).map((rp) -> rp.getRefine()).orElse(0);
                    int level = player.experienceLevel;
                    rankDamageBonus = (float)Math.max(rankDamageBonus, (double)Math.min(level, refine) * SlashBladeConfig.REFINE_DAMAGE_MULTIPLIER.get());
                }

                damageValue += rankDamageBonus;
            }

            damageValue = (float)((double)damageValue * (double) AttackManager.getSlashBladeDamageScale(living) * SlashBladeConfig.SLASHBLADE_DAMAGE_MULTIPLIER.get());
            if (this.getIsCritical()) {
                damageValue += (float)this.random.nextInt(Mth.ceil(damageValue) / 2 + 2);
            }
        }

        targetEntity.heal( damageValue);

            this.playSound(this.getHitEntitySound(), 1.0F, 1.2F / (this.random.nextFloat() * 0.2F + 0.9F));


    }
    }

}
